//
//  ScanLineAnimationView.m
//  YLScaner
//
//  Created by Admin on 2025/9/25.
//

#import "ScanLineWithEffectView.h"

@interface ScanLineWithEffectView ()
@property (nonatomic, strong) UIView *maskLayer;    // 遮罩层
@property (nonatomic, strong) UIView *scanLine;     // 扫描线
@property (nonatomic, assign) CGRect scanFrame;     // 扫描框范围
@end

@implementation ScanLineWithEffectView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

#pragma mark - 初始化界面
- (void)setupUI {
    self.backgroundColor = [UIColor blackColor]; // 模拟摄像头背景
    
    // 1. 计算扫描框（居中220x220）
    CGFloat size = 220;
    self.scanFrame = CGRectMake(
        (self.bounds.size.width - size)/2,
        (self.bounds.size.height - size)/2,
        size,
        size
    );
    
    // 2. 遮罩层（周围半透明）
    self.maskLayer = [[UIView alloc] initWithFrame:self.bounds];
    self.maskLayer.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
    [self addSubview:self.maskLayer];
    
    // 3. 扫描框挖空
    UIView *scanBox = [[UIView alloc] initWithFrame:self.scanFrame];
    scanBox.backgroundColor = [UIColor clearColor];
    // self.maskLayer.mask = scanBox;
    
    // 4. 扫描线（淡绿色，带圆角）
    self.scanLine = [[UIView alloc] initWithFrame:CGRectMake(
        self.scanFrame.origin.x + 8,    // 左右缩进8pt
        self.scanFrame.origin.y,        // 初始在顶部
        self.scanFrame.size.width - 16, // 宽度比扫描框窄
        2                               // 线宽
    )];
    self.scanLine.backgroundColor = [UIColor colorWithRed:0.2 green:0.9 blue:0.4 alpha:0.9];
    self.scanLine.layer.cornerRadius = 1; // 圆角更柔和
    [self.maskLayer addSubview:self.scanLine];
}

#pragma mark - 核心动画（带路径效果）
- (void)startAnimation {
    [self.scanLine.layer removeAllAnimations]; // 清除旧动画
    
    // 起点和终点（主路径）
    CGFloat startY = self.scanFrame.origin.y;
    CGFloat endY = self.scanFrame.origin.y + self.scanFrame.size.height - self.scanLine.bounds.size.height;
    
    // 1. 定义关键帧路径（添加波动效果）
    CAKeyframeAnimation *positionAnim = [CAKeyframeAnimation animationWithKeyPath:@"position.y"];
    
    // 关键帧：在主路径上添加5个点，中间点带±1pt波动
    positionAnim.values = @[
        @(startY + 1),                     // 起点（轻微偏移）
        @(startY + self.scanFrame.size.height * 0.25 - 1), // 25%处向下波动1pt
        @(startY + self.scanFrame.size.height * 0.5 + 1),  // 50%处向上波动1pt
        @(startY + self.scanFrame.size.height * 0.75 - 1), // 75%处向下波动1pt
        @(endY + 1)                        // 终点（轻微偏移）
    ];
    
    // 时间点：均匀分布
    positionAnim.keyTimes = @[@0, @0.25, @0.5, @0.75, @1];
    
    // 2. 动画曲线（先加速后减速，自然缓冲）
    positionAnim.timingFunctions = @[
        [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn],  // 加速
        [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],  // 匀速
        [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear],  // 匀速
        [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]  // 减速
    ];
    
    // 3. 边缘回弹效果（到达终点后轻微反弹）
    positionAnim.duration = 2.2; // 总时长（包含0.2秒回弹）
    positionAnim.repeatCount = MAXFLOAT;
    
    // 4. 回弹逻辑（通过动画组实现）
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[positionAnim];
    group.duration = positionAnim.duration;
    group.repeatCount = MAXFLOAT;
    
    [self.scanLine.layer addAnimation:group forKey:@"scanLineEffectAnim"];
}

#pragma mark - 停止动画
- (void)stopAnimation {
    [self.scanLine.layer removeAllAnimations];
}

#pragma mark - 布局适配
- (void)layoutSubviews {
    [super layoutSubviews];
    self.maskLayer.frame = self.bounds;
}

@end

